﻿package mortal.game.view.battlefield
{
    import __AS3__.vec.*;
    import com.mui.containers.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.cache.battlefield.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class BattlefieldResultWindow extends BaseWindow
    {
        private var _tabBarData:Array;
        private var _vcTitleItem:Vector.<TitleItem>;
        private var _resultList:Vector.<BattlefieldResult>;
        private const pageSize:int = 10;
        private var _tabBar:GTabarNew;
        private var _titleBox:GBox;
        private var _tileList:GTileList;
        private var _pageSelecter:PageSelecter;
        private var _myTotalHonor:GTextFiled;
        private var _honorShop:GButton;
        private var _myKill:GTextFiled;
        private var _myAssist:GTextFiled;
        private var _myBeKill:GTextFiled;
        private var _myRank:GTextFiled;
        private var _showMyScore:GTextFiled;
        private var _btnLeave:GButton;
        private static var _instance:BattlefieldResultWindow;

        public function BattlefieldResultWindow(param1:ILayer = null)
        {
            this._resultList = new Vector.<BattlefieldResult>;
            super(param1);
            setSize(800, 500);
            title = Language.getString(11320);
            return;
        }// end function

        override protected function configUI() : void
        {
            this._tabBarData = new Array();
            this._tabBarData.push({label:Language.getString(11321), value:0, name:"0"});
            this._tabBarData.push({label:GameDefConfig.instance.getBattlefieldForceInfo(8).text, value:8, name:"8"});
            this._tabBarData.push({label:GameDefConfig.instance.getBattlefieldForceInfo(9).text, value:9, name:"9"});
            this._tabBarData.push({label:GameDefConfig.instance.getBattlefieldForceInfo(10).text, value:10, name:"10"});
            this._vcTitleItem = new Vector.<TitleItem>;
            this._vcTitleItem.push(new TitleItem(Language.getString(11322), 60));
            this._vcTitleItem.push(new TitleItem(Language.getString(11323), 130));
            this._vcTitleItem.push(new TitleItem(Language.getString(11324), 70));
            this._vcTitleItem.push(new TitleItem(Language.getString(11325), 55));
            this._vcTitleItem.push(new TitleItem(Language.getString(11326), 50));
            this._vcTitleItem.push(new TitleItem(Language.getString(11327), 50));
            this._vcTitleItem.push(new TitleItem(Language.getString(11328), 55));
            this._vcTitleItem.push(new TitleItem(Language.getString(11329), 65));
            this._vcTitleItem.push(new TitleItem(Language.getString(11330), 65));
            this._vcTitleItem.push(new TitleItem(Language.getString(11331), 65));
            this._vcTitleItem.push(new TitleItem(Language.getString(11332), 65));
            this._vcTitleItem.push(new TitleItem(Language.getString(11333), 67));
            super.configUI();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._tabBar = UIFactory.gTabBarNew(2, 30, this._tabBarData, 800, 500, 65, 26, this, this.onTabBarSelectedChange, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            pushUIToDisposeVec(UIFactory.bg(10, 62, 785, 370, this, "PanelBg"));
            pushUIToDisposeVec(UIFactory.bg(10, 62, 785, 22, this, "RegionTitleBg"));
            this._titleBox = ObjTeamCreate.createTitles(this._vcTitleItem, GlobalStyle.textFormatBai.center());
            UIFactory.setObjAttri(this._titleBox, 8, 62, -1, -1, this);
            this._tileList = UIFactory.tileList(8, 85, 780, 307, this);
            this._tileList.setStyle("cellRenderer", BattlefieldResultCellRenderer);
            this._tileList.rowHeight = 30;
            this._tileList.columnWidth = 780;
            this._tileList.direction = GBoxDirection.VERTICAL;
            this._pageSelecter = UIFactory.pageSelecter(328, 398, this, PageSelecter.CompleteMode);
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            pushUIToDisposeVec(UIFactory.bg(10, 436, 785, 58, this, "PanelBg"));
            this._myTotalHonor = UIFactory.gTextField(Language.getStringByParam(11334, "0"), 30, 456, 130, 20, this, GlobalStyle.textFormatPutong);
            this._honorShop = UIFactory.gButton(Language.getString(11335), 165, 454, 65, 22, this, "GreenLinkButton");
            this._myKill = UIFactory.gTextField(Language.getStringByParam(11336, "0"), 257, 456, 80, 20, this, GlobalStyle.textFormatHuang);
            this._myAssist = UIFactory.gTextField(Language.getStringByParam(11337, "0"), 340, 456, 80, 20, this, GlobalStyle.textFormatHuang);
            this._myRank = UIFactory.gTextField(Language.getStringByParam(11338, "100"), 410, 456, 140, 20, this, GlobalStyle.textFormatPutong);
            this._showMyScore = UIFactory.gTextField(Language.getString(11339), 550, 456, 150, 20, this, GlobalStyle.textFormatHuang);
            this._showMyScore.htmlText = Language.getString(11340);
            this._btnLeave = UIFactory.gButton(Language.getString(11341), 710, 456, 49, 22, this);
            this._btnLeave.visible = false;
            this._showMyScore.configEventListener(TextEvent.LINK, this.onShowMyScore);
            this._honorShop.configEventListener(MouseEvent.CLICK, this.onClickHonorShop);
            this._btnLeave.configEventListener(MouseEvent.CLICK, this.onClickBtnLeave);
            this.updateScore();
            NetDispatcher.addCmdListener(ServerCommand.HonorUpdate, this.onHonorUpdate);
            return;
        }// end function

        protected function onHonorUpdate(param1) : void
        {
            this._myTotalHonor.htmlText = Language.getStringByParam(11334, Cache.instance.role.money.honor);
            return;
        }// end function

        protected function onClickHonorShop(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.NPC_OpenNpcShop, ShopType.Honor));
            return;
        }// end function

        protected function onClickBtnLeave(event:MouseEvent) : void
        {
            if (GameMapUtil.curMapState.isBattlefield)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyClickQuickBtn));
            }
            this.hide();
            return;
        }// end function

        override protected function closeBtnClickHandler(event:MouseEvent) : void
        {
            super.closeBtnClickHandler(event);
            return;
        }// end function

        protected function onShowMyScore(event:TextEvent) : void
        {
            Alert.show(Language.getString(11342), null, Alert.OK | Alert.CANCEL, null, this.onChooseShowMyScore);
            return;
        }// end function

        protected function onChooseShowMyScore(param1:int) : void
        {
            if (param1 == Alert.OK)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.BattlefieldShowMyScore));
            }
            return;
        }// end function

        public function updateScore() : void
        {
            this._tabBar.selectedIndex = 0;
            this.onTabBarSelectedChange();
            this.updateMyScore();
            this._myTotalHonor.htmlText = Language.getStringByParam(11334, Cache.instance.role.money.honor);
            return;
        }// end function

        protected function updateMyScore() : void
        {
            var _loc_1:* = Cache.instance.battlefieldCache.myResult;
            if (_loc_1)
            {
                this._myKill.text = Language.getStringByParam(11336, _loc_1.score.killPlayer.toString());
                this._myAssist.text = Language.getStringByParam(11337, _loc_1.score.assist.toString());
                this._myRank.text = Language.getStringByParam(11338, _loc_1.rank.toString());
            }
            if (Cache.instance.battlefieldCache.battleFieldResult)
            {
                this._btnLeave.visible = Cache.instance.battlefieldCache.battleFieldResult.isFinal;
            }
            return;
        }// end function

        protected function onTabBarSelectedChange(event:Event = null) : void
        {
            this._pageSelecter.currentPage = 1;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    this._resultList = Cache.instance.battlefieldCache.battlefieldTotalResult;
                    break;
                }
                case 1:
                {
                    this._resultList = Cache.instance.battlefieldCache.battlefieldCamp1Result;
                    break;
                }
                case 2:
                {
                    this._resultList = Cache.instance.battlefieldCache.battlefieldCamp2Result;
                    break;
                }
                case 3:
                {
                    this._resultList = Cache.instance.battlefieldCache.battlefieldCamp3Result;
                    break;
                }
                default:
                {
                    break;
                }
            }
            this._pageSelecter.maxPage = Math.ceil(this._resultList.length / 10);
            this.updateList();
            return;
        }// end function

        protected function onPageChange(event:Event = null) : void
        {
            this.updateList();
            return;
        }// end function

        protected function updateList() : void
        {
            var _loc_1:* = new DataProvider();
            var _loc_2:* = (this._pageSelecter.currentPage - 1) * this.pageSize;
            var _loc_3:* = Math.min(this.pageSize * this._pageSelecter.currentPage, this._resultList.length);
            var _loc_4:* = _loc_2;
            while (_loc_4 < _loc_3)
            {
                
                _loc_1.addItem(this._resultList[_loc_4]);
                _loc_4++;
            }
            this._tileList.dataProvider = _loc_1;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._resultList = new Vector.<BattlefieldResult>;
            this._tabBar.dispose(param1);
            this._tabBar = null;
            this._titleBox.dispose(param1);
            this._titleBox = null;
            this._tileList.dispose(param1);
            this._tileList = null;
            this._pageSelecter.dispose(param1);
            this._pageSelecter = null;
            this._myTotalHonor.dispose(param1);
            this._myTotalHonor = null;
            this._honorShop.dispose(param1);
            this._honorShop = null;
            this._myKill.dispose(param1);
            this._myKill = null;
            this._myAssist.dispose(param1);
            this._myAssist = null;
            this._myRank.dispose(param1);
            this._myRank = null;
            this._showMyScore.dispose(param1);
            this._showMyScore = null;
            this._btnLeave.dispose(param1);
            this._btnLeave = null;
            NetDispatcher.removeCmdListener(ServerCommand.HonorUpdate, this.onHonorUpdate);
            super.disposeImpl(param1);
            return;
        }// end function

        public static function get instance() : BattlefieldResultWindow
        {
            if (!_instance)
            {
                _instance = new BattlefieldResultWindow;
            }
            return _instance;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
